package com.yz.springtest.resource;

import java.io.IOException;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.FileCopyUtils;

public class FileSourceExample {
	 public static void main(String[] args) {
		try {
//			String filePath = "D:/project/myspace/chapter4/src/main/resources/conf/file1.txt";
//			//1、使用系统文件路径加载文件
//			WritableResource res1 = new PathResource(filePath);
//			
//			//2、使用类路径加载文件
//			ClassPathResource res2 = new ClassPathResource("conf/file1.txt");
//			
//			//3、WritableResource接口写资源文件
//			OutputStream stream1 = res1.getOutputStream();
//			stream1.write("欢迎！！！".getBytes());
//			stream1.close();
//			
//			//Resource接口读资源文件
//			InputStream ins1 = res1.getInputStream();
//			InputStream ins2 = res2.getInputStream();
//			
//			ByteArrayOutputStream baos = new ByteArrayOutputStream();
//			int i;
//			while ((i = ins1.read()) != -1) {
//				baos.write(i);
//			}
//			System.out.println(baos.toString());
//			
//			System.out.println("res1:" + res1.getFilename());
//			System.out.println("res2:" +  res2.getFilename());
			
			Resource res = new ClassPathResource("conf/file1.txt");
			EncodedResource encRes = new EncodedResource(res, "UTF-8");
			String copyToString = FileCopyUtils.copyToString(encRes.getReader());
			System.out.println(copyToString);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
